function changeAvatar() {
    const avatar = document.getElementById("avatar")
    let num = 0
    avatar.onclick = function () {
        num++
        avatar.src =`../img/${num}.jpg`
        sessionStorage.setItem("avatarSrc", avatar.src)
        //小头像切换
        const avatarSrc = sessionStorage.getItem("avatarSrc")
        const smAvatar = document.getElementById("smAvatar")
        smAvatar.src = avatarSrc
        if(num == 9)num = 0

    }
}
changeAvatar()

//渲染个性签名
const qm = document.getElementById("qm")
function fun() {
    const signature = qm.value
    const text = document.getElementById("text")
    text.innerHTML = signature

}
qm.oninput = debounce(fun,1000)
function debounce(fn,time){
    let timer = null
    return function(){
        if(timer){
            clearTimeout(timer)
        }
        setTimeout(() => {
            fn()
        }, time);
    }
}
qm.onblur = function(){
    const text = qm.value
    Ajax({
        url:"http://localhost/project/server/signatureUpdate.php",
        data:{text},
        success(){
            location.reload()
        }
    })
}

//修改信息
//修改用户名
function updateUser(){
const username1 = document.querySelector("#username1").value
if(!username1){
    return alert("用户名不能为空!")
}
sessionStorage.setItem("username",username1)
Ajax({
    url:"http://localhost/project/server/updateName.php",
    data:{name1:username1},
    success(){
        alert("修改成功!")
        location.reload()
    }
})
}
//修改密码
function updateInfo(){
    const password1 = document.querySelector("#password1").value
    const password2 = document.querySelector("#password2").value
    const password3 = document.querySelector("#password3").value
    const password = JSON.parse(sessionStorage.getItem("manageInfo"))[0].password
    if(!(password1&&password2&&password3)){
        return alert("密码不能为空!")
    }
    if(password1 != password){
        return alert("原密码输入错误!")
    }
    if(!(/^\w{5,16}$/.test(password2))){
        return alert("密码格式错误!")
    }
    if(password2 != password3){
        return alert("两次输入的密码不同!")
    }
    Ajax({
        url:"http://localhost/project/server/updatePassword.php",
        data:{password:password2},
        success(){
            alert("修改成功!")
            location.reload()
        }
    })
}

//模态框选择头像
function avatarSelect(){
    const box = document.querySelector(".box")
    const imgs = document.querySelectorAll(".box img")
    box.onclick = function(e){
        if(e.target.nodeName == "IMG"){
            for(let i = 0;i < 9;i++){
                imgs[i].classList.remove("active")
            }
            e.target.classList.add("active")
        }
    }
}
avatarSelect()
//点击确认渲染头像
function chooseImg(){
    const img = document.querySelectorAll(".box img[class='active']")
    const avatar = document.getElementById("avatar")
    const smAvatar = document.getElementById("smAvatar")
    sessionStorage.setItem("avatarSrc",img[0].src)
    avatar.src = sessionStorage.getItem("avatarSrc")
    location.reload()
}
